#include "Tool.h"

Tool::Tool(DiagramMan& dman)
:	_dman(dman),
	_dragging(false),
	// default fine for _initialPress
	_pressHandled(false)
{
}

DiagramMan& Tool::dman() const
{
	return _dman;
}

bool Tool::mouseMoveEvent(QMouseEvent& ev)
{
	if (_dragging)
	{
		drag(ev);

		return true;
	}
	else if (ev.buttons().testFlag(Qt::LeftButton))
	{
		// we just started dragging then
		_dragging = true;

		startDrag(_initialPress);
		drag(ev);

		return true;
	}
	else if (!ev.buttons())
	{
		move(ev);

		return true;
	}
	else
		return false;
}

bool Tool::mousePressEvent(QMouseEvent& ev)
{
	if (ev.button() == Qt::LeftButton)
	{
		_initialPress = ev.pos();

		return _pressHandled = press(ev);
	}

	return false;
}

bool Tool::mouseReleaseEvent(QMouseEvent& ev)
{
	if (ev.button() != Qt::LeftButton)
		return false;

	if (_dragging)
	{
		endDrag(ev);
		_dragging = false;
	}
	else if (!_pressHandled)
	{
		click(ev);
	}

	return true;
}

bool Tool::press(QMouseEvent&)
{
	return false;
}

void Tool::move(QMouseEvent&)
{
	// just make it optional for others
}

